package cn.information.view.extend;

import cn.information.bean.Supplier;
import cn.information.dao.SupplierDao;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.util.Vector;

public class SupplierQueryResultUI extends JFrame {
    private JScrollPane jScrollPane = new JScrollPane();
    private AbstractTableModel tableModel;
    private Vector<Vector> vector = new Vector<>();

    public SupplierQueryResultUI(Supplier supplier) {
        setTitle("供应商查询结果 - " + supplier.getGysname());
        setBounds(300, 200, 600, 300);
        setLayout(new BorderLayout());

        JPanel panelUp = new JPanel();
        JPanel panelDown = new JPanel();

        panelUp.add(jScrollPane);
        createTable(supplier);

        add(panelUp, BorderLayout.CENTER);
        add(panelDown, BorderLayout.SOUTH);

        JButton btnBack = new JButton("返回");
        btnBack.addActionListener(e -> {
            new SupplierQueryUI().setVisible(true);
            dispose();
        });
        panelDown.add(btnBack);

        JButton btnExit = new JButton("退出");
        btnExit.addActionListener(e -> dispose());
        panelDown.add(btnExit);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    private void createTable(Supplier supplier) {
        String[] columnNames = {"供应商ID", "供应商姓名", "电话", "地址", "商品名称"};
        JTable table = new JTable();
        jScrollPane.setViewportView(table);

        Vector<Object> v = new Vector<>();
        v.add(supplier.getGysid());
        v.add(supplier.getGysname());
        v.add(supplier.getGysphone());
        v.add(supplier.getAddress());
        v.add(supplier.getSpname());
        vector.add(v);

        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (rowIndex < vector.size()) {
                    Vector v = vector.get(rowIndex);
                    return v.get(columnIndex);
                } else {
                    return null;
                }
            }
        };
        table.setModel(tableModel);
    }
}